<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category TagController.php
 * @Depiction:
 * Author: hwy
 * Date: 2021/2/1  14:51
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Http\Requests\TagStoreRequest;
use App\Http\Requests\TagUpdateRequest;
use App\Services\TagService;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;

class TagController extends Controller
{
    /**
     * @var string
     */
    protected $_model = 'tag';

    /**
     * 文章标签服务类
     * @var TagService
     */
    protected $tagService;

    /**
     * TagController constructor.
     */
    public function __construct()
    {
        $this->tagService = TagService::getInstance();
    }

    /**
     * @param Request $request
     * @return Application|Factory|View
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/3/8  17:11
     */
    public function index(Request $request)
    {
        $compact = compact('request');

        return view('admin.' . $this->_model . '.index', $compact);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/3/8  17:12
     */
    public function getList(Request $request): JsonResponse
    {
        $responseData = $this->tagService->getList($request);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  17:00
     */
    public function create(Request $request)
    {
        $compact = compact('request');

        return view('admin.' . $this->_model . '.create', $compact);
    }

    /**
     * @param TagStoreRequest $request
     * @return JsonResponse
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/3/8  17:13
     */
    public function store(TagStoreRequest $request): JsonResponse
    {
        $responseData = $this->tagService->addData($request);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @param $id
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  17:00
     */
    public function edit(Request $request, $id)
    {
        $data = $this->tagService->getOne($id);

        return view('admin.' . $this->_model . '.edit', compact('request', 'data'));

    }

    /**
     * @param TagUpdateRequest $request
     * @param $id
     * @return JsonResponse
     * @Depiction:
     * @Author: hwy
     * @Date: 2021/3/8  17:13
     */
    public function update(TagUpdateRequest $request, $id): JsonResponse
    {
        $responseData = $this->tagService->editData($request, $id);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2021/2/24  16:38
     */
    public function destroy(Request $request): JsonResponse
    {
        $ids = $request->id;
        $responseData = $this->tagService->delData($ids);

        return response()->json($responseData);
    }

}
